<template>
	<div class="user-edit-container">
		<!-- Header 区域 -->
		<van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" fixed />

		<!-- 用户资料 -->
		<van-cell-group class="action-card">
			<van-cell title="头像" is-link center>
				<template #default>
					<van-image round class="avatar" :src="profile.photo" @click="$refs.iptFile.click()" />
					<!-- file 选择框 -->
					<input type="file" ref="iptFile" v-show="false" accept="image/*" @change="onFileChange" />
				</template>
			</van-cell>
			<van-cell title="名称" is-link :value="profile.name" @click="showNameDialogFn" />
			<van-dialog v-model="isShowNameDialog" title="修改名称" show-cancel-button
				:before-close="onNameDialogBeforeClose">
				<!-- 输入框 -->
				<van-field v-model.trim="userName" input-align="center" maxlength="7" placeholder="请输入名称" v-fofo />
			</van-dialog>
			<van-cell title="生日" is-link :value="profile.birthday" @click="showBirthdayFn" />
		</van-cell-group>
		<van-popup v-model="isShowBirth" position="bottom" style="height: 50%" round>
			<!-- 日期选择控件 -->
			<van-datetime-picker v-model="currentDate" type="date" title="选择出生日期" :min-date="minDate"
				:max-date="maxDate" :show-toolbar="true" @cancel="isShowBirth = false" @confirm="confirmFn" />
		</van-popup>
	</div>
</template>

<script>
import { userInfo, updatePhotoAPI, updateProfileAPI } from '@/api'
import { Notify } from 'vant'
import { formatDate } from '@/utils/date.js'
export default {
  name: 'UserEdit',
  data () {
    return {
      profile: {},
      isShowNameDialog: false,
      userName: '',
      isShowBirth: false,
      minDate: new Date(1900, 0, 1), // 最小的可选的日期
      maxDate: new Date(), // 最大的可选日期
      currentDate: new Date() // 当前日期

    }
  },
  created () {
    this.getUserInfo()
  },
  methods: {
    async getUserInfo () {
      const res = await userInfo()
      this.profile = res.data.data
    },
    async onFileChange (ev) {
      //   console.log(ev.target.files[0])
      if (ev.target.files.length === 0) return // 防止用户未选择图片
      const fd = new FormData()
      fd.append('photo', ev.target.files[0]) // photo在表单里参数名携带
      const res = await updatePhotoAPI(fd)
      console.log(res)
      this.profile.photo = res.data.data.photo // 更新最新头像
    },
    async onNameDialogBeforeClose (action, done) {
      // action的值: confirm或cancel(点击按钮区分)
      if (action === 'confirm') {
        // 确定
        // unicode编码 \u4092
        // url编码 %E2%C3%D1
        if (/^[A-Za-z0-9\u4e00-\u9fa5]{1,7}$/.test(this.userName)) {
          await updateProfileAPI({ userName: this.userName })
          this.profile.name = this.userName

          done()
        } else {
          // 提示用户
          Notify({ type: 'warning', message: '用户名中英文和数字1-7位' })
          // 阻止弹窗关闭
          done(false)
        }
      } else if (action === 'cancel') {
        // 取消
        done()
      }
    },
    showNameDialogFn () {
      this.isShowNameDialog = true
      this.userName = this.profile.name
    },
    showBirthdayFn () {
      this.isShowBirth = true
      this.currentDate = new Date(this.profile.birthday) // 设置data组件默认显示时间
    },
    async confirmFn () {
      // console.log(this.currentDate instanceof Date)
      // this.currentDate里值是日期对象
      // 但是后台要"年-月-日"格式字符串参数值

      console.log(this.currentDate)
      const dateStr = formatDate(this.currentDate)
      await updateProfileAPI({
        birthday: dateStr
      })
      // 前端页面同步
      this.profile.birthday = dateStr
      // 时间选择器关闭
      this.isShowBirth = false
    }
  }
}
</script>

<style lang="less" scoped>
::v-deep .van-nav-bar {
	background-color: #007bff;
}

// ::v-deep .van-ellipsis {
// 	color: white;
// }

.user-edit-container {
	padding-top: 46px;

	.avatar {
		width: 50px;
		height: 50px;
	}
}

::v-deep .van-picker {
	color: black
}

::v-deep .van-icon-arrow-left {
	color: white
}
</style>
